﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Goods
{
    /// <summary>
    /// GoodsEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsEditWindow : Window
    {
        private ShoesService shoesService = ServiceContainer.GetService<ShoesService>();
        private VendorService vendorService = ServiceContainer.GetService<VendorService>();
        private string imagePath = null;

        public Shoes Shoes { get; set; }

        public GoodsEditWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Title = "编辑商品";
            if (this.Shoes == null)
            {
                this.Shoes = new Shoes();
                this.Title = "新增商品";
            }

            try
            {
                //读取Vendor
                Mengsk.ProxyDelivery.Domain.Vendor[] vendors = this.vendorService.GetPageByMap(new Dictionary<string, object>(), 0, 0).Datas.OrderBy(obj => obj.Name).ToArray();
                this.cbbVendors.ItemsSource = vendors;

                //选择厂家
                if (this.Shoes.VendorId > 0)
                {
                    this.cbbVendors.SelectedItem = vendors.FirstOrDefault(obj => obj.Id == this.Shoes.VendorId);
                }

                //选择类型
                if (this.Shoes.Type != ShoesType.OTHER)
                {
                    this.cbbTypes.SelectedIndex = (int)this.Shoes.Type - 1;
                }

                this.tbNumber.Text = this.Shoes.Number;
                this.tbPrice.Text = this.Shoes.Price.ToString("F0");
                this.tbUrl.Text = this.Shoes.Url;
                this.tbWeight.Text = this.Shoes.Weight.ToString("F2");
                this.chkEnabled.IsChecked = this.Shoes.Enabled;
                this.chkUpdateEnabled.IsChecked = this.Shoes.UpdateEnabled;
                this.imagePath = this.Shoes.Image;
                this.img.Source = (new Converters.WebUrlImageConverter()).Convert(this.Shoes.Image, null, null, null) as ImageSource;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnChooseImage_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            bool? ret = ofd.ShowDialog();
            if (ret == null || ret.Value == false)
            {
                return;
            }

            this.img.Source = new BitmapImage(new Uri(ofd.FileName));
            this.imagePath = ofd.FileName;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Mengsk.ProxyDelivery.Domain.Vendor vendor = this.cbbVendors.SelectedItem as Mengsk.ProxyDelivery.Domain.Vendor;
                int typeIndex = this.cbbTypes.SelectedIndex;
                string number = this.tbNumber.Text.Trim();
                float price = float.Parse(this.tbPrice.Text.Trim());
                float weight = float.Parse(this.tbWeight.Text.Trim());
                string url = this.tbUrl.Text.Trim();
                bool enabled = this.chkEnabled.IsChecked == null ? false : this.chkEnabled.IsChecked.Value;
                bool updateEnabled = this.chkUpdateEnabled == null ? false : this.chkUpdateEnabled.IsChecked.Value;

                if (vendor == null)
                {
                    MessageBox.Show("请选择厂家");
                    return;
                }

                if (typeIndex < 0)
                {
                    MessageBox.Show("选择类型");
                    return;
                }

                if (string.IsNullOrWhiteSpace(number))
                {
                    MessageBox.Show("请输入货号");
                    return;
                }

                this.Shoes.VendorId = vendor.Id;
                this.Shoes.Type = (ShoesType)(typeIndex + 1);
                this.Shoes.Number = number;
                this.Shoes.Price = price;
                this.Shoes.Url = url;
                this.Shoes.Weight = weight;
                this.Shoes.Enabled = enabled;
                this.Shoes.UpdateEnabled = updateEnabled;
                this.Shoes.Image = imagePath;
                if (this.Shoes.Id < 1)
                {
                    this.Shoes.Id = this.shoesService.Insert(this.Shoes);
                }
                else
                {
                    this.shoesService.Update(this.Shoes);
                }
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
